package game;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.BorderFactory;
import javax.swing.JPanel;

import datastructures.Coordinate;
import datastructures.Edge;
import datastructures.Word;
import game.graphics.GEdge;
import game.graphics.GWord;

/**
 * @author Fredrik
 *
 */
public class GameFieldPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2847404054182791049L;

	/** Width of the work panel */
	public static final int WIDTH = 800;
	
	/** Height of the work panel */
	public static final int HEIGHT = 600;
	
	/** Width of the middle rectangle frame of the work panel */
	public static final int MIDDLE_REC_WIDTH = 640;
	
	/** Height of the middle rectangle frame of the work panel */
	public static final int MIDDLE_REC_HEIGHT = 480;

	private MainModel model;

	/**
	 * Constructor creating a game field panel
	 * @param model
	 */
	public GameFieldPanel(MainModel model) {
		this.model = model;
		//Border border = BorderFactory.createEmptyBorder(0, 0, HEIGHT, WIDTH);
		//this.setBorder(border);
		this.setPreferredSize(new Dimension(GameFieldPanel.WIDTH,GameFieldPanel.HEIGHT));
		this.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		this.setBounds(0, 0, WIDTH, HEIGHT);
		this.setAlignmentX(0);
		this.setAlignmentY(0);
		
		//this.setBackground(Color.ORANGE);
		//this.setBackground(new Color(255, 255, 255));
	}

	/** (non-Javadoc)
	 * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
	 */
	@Override
	public void paintComponent(Graphics g) {
		if(this.model.getEditingMode() || this.model.isWindowUnlocked(this.model.getCurrentWindowCordinates())){
			setBackground(Color.YELLOW);
		}else{
			setBackground(Color.ORANGE);
			super.paintComponent(g);
			paintWorkSpace(g);
			return;
		}
		super.paintComponent(g);
		paintWorkSpace(g);
		
		ArrayList<GWord> myGraphicWords = new ArrayList<GWord>();
		
		Iterator<Word> wordIterator = this.model.getWordsForCurrentWindow().iterator();
		
		while (wordIterator.hasNext()) {
			Word theWord = wordIterator.next();
			GWord newGWord = new GWord(theWord);
			newGWord.setEditingMode(this.model.getEditingMode());
			Iterator<Edge> edgeIterator = theWord.getNeighbourList().iterator();

			while (edgeIterator.hasNext()) {
				Edge theEdge = edgeIterator.next();
				GEdge newGEdge = new GEdge(theEdge);
				newGEdge.setIsEditing(this.model.getEditingMode());
				newGEdge.paint(g);
			}

			myGraphicWords.add(newGWord);
		}

		Iterator<GWord> gWordIterator = myGraphicWords.iterator();
		while (gWordIterator.hasNext()) {
			gWordIterator.next().paint(g);
		}

	}

	/**
	 * Returns the word at a place with the coordinates c from the game field
	 * @param c
	 * @return the word at a place with the coordinates c from the game field
	 */
	public Word getWord(Coordinate c) {
		
		Iterator<Word> it = this.model.getWordsForCurrentWindow().iterator();
		while (it.hasNext()) {
			GWord newGWord = new GWord(it.next());
			if (newGWord.inBoundaryBox(c)) {
				return newGWord.getWord();
			}
		}
		return null;
	}

	private void paintWorkSpace(Graphics g) {
		
		int middleRecX = (WIDTH - MIDDLE_REC_WIDTH) / 2;
		int middleRecY = (HEIGHT - MIDDLE_REC_HEIGHT) / 2;

		g.drawRect(middleRecX, middleRecY, MIDDLE_REC_WIDTH, MIDDLE_REC_HEIGHT);

		if (this.model.getEditingMode()) {
			g.fillRect(0, 0, middleRecX, middleRecY);
			g.fillRect(middleRecX + MIDDLE_REC_WIDTH, 0, middleRecX, middleRecY);
			g.fillRect(middleRecX + MIDDLE_REC_WIDTH, middleRecY
					+ MIDDLE_REC_HEIGHT, middleRecX, middleRecY);
			g.fillRect(0, middleRecY + MIDDLE_REC_HEIGHT, middleRecX, middleRecY);
		}
	}
}
